import { ThunkAction } from "redux-thunk";
import { RootState } from "../store";
import { ProdctCollection_List } from "../../Utils/Http";
import {
  Constant
} from "../constan";


type PRODUCIS_START = {
  type: typeof Constant.FETCH_RECOMMEND_PRODUCIS_START;
};
type PRODUCIS_SUCCESS = {
  type: typeof Constant.FETCH_RECOMMEND_PRODUCIS_SUCCESS;
  payload: any;
};
type PRODUCIS_FAIL = {
  type: typeof Constant.FETCH_RECOMMEND_PRODUCIS_FAIL;
  payload: any;
};

export type RecommentdAction =
  | PRODUCIS_START
  | PRODUCIS_SUCCESS
  | PRODUCIS_FAIL;

// 正在调用推荐列表api的action
export const fetchRecommendProducStartAction = (): PRODUCIS_START => {
  return {
    type: Constant.FETCH_RECOMMEND_PRODUCIS_START,
  };
};

// 调用推荐列表api成功的action
export const fetchRecommendProducSuccessAction = (
  data: any
): PRODUCIS_SUCCESS => {
  return {
    type: Constant.FETCH_RECOMMEND_PRODUCIS_SUCCESS,
    payload: data,
  };
};

// 调用推荐列表api失败的action
export const fetchRecommendProducFailAction = (error: any): PRODUCIS_FAIL => {
  return {
    type: Constant.FETCH_RECOMMEND_PRODUCIS_FAIL,
    payload: error,
  };
};

// 异步获取推荐列表数据
export const giveMeProdcutDataAction = (): ThunkAction<
  void,
  RootState,
  {api:string,whatever: number},
  RecommentdAction
> => async (dispatch, getState, extraArgument) => {
  // console.log("extraArgument",extraArgument.api) thunk.withExtraArgument传递的参数集合
  dispatch(fetchRecommendProducStartAction());
  try {
    const data = await ProdctCollection_List();
    dispatch(fetchRecommendProducSuccessAction(data));
  } catch (error) {
    dispatch(fetchRecommendProducFailAction(error));
  }
};
